<template>
  <div class="user-select">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="userList" @row-click="handleRowClick">
      <el-table-column label="单选" align="center" width="60">
        <template slot-scope="scope">
          <el-radio v-model="selectedUserId" :label="scope.row.userId">&nbsp;</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" />
      <el-table-column label="部门" align="center" prop="dept.deptName" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div slot="footer" class="dialog-footer" style="text-align: right; margin-top: 20px;">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleConfirm" :disabled="!selectedUserId">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { listUser } from "@/api/system/user"

export default {
  name: "UserSelect",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 选中的用户ID
      selectedUserId: null,
      // 选中的用户对象
      selectedUser: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true
      listUser(this.queryParams).then(response => {
        this.userList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 行点击事件 */
    handleRowClick(row) {
      this.selectedUserId = row.userId
      this.selectedUser = row
    },
    /** 确定选择 */
    handleConfirm() {
      if (this.selectedUserId && this.selectedUser) {
        // 按照 nick_name(user_name) 的格式返回显示名称
        const userDisplayName = `${this.selectedUser.nickName}(${this.selectedUser.userName})`
        this.$emit('userSelected', {
          userId: this.selectedUser.userId,
          displayName: userDisplayName
        })
      }
    },
    /** 取消选择 */
    handleCancel() {
      this.$emit('userSelected', null)
    }
  }
}
</script>

<style scoped>
.user-select {
  padding: 20px;
}
.dialog-footer {
  margin-top: 20px;
  text-align: right;
}
</style>